/**
 * Copyright 2016-2017 TVI Go Easy.
 * Created on 2017/4/17 22:50
 */
package org.mechanic.fund.web.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.mechanic.core.web.MediaTypes;
import org.mechanic.fund.domain.system.manager.Department;
import org.mechanic.fund.service.product.impl.DepartmentService;
import org.mechanic.fund.web.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 部门信息 API
 *
 * @author mechanic
 * @version 0.0.1
 */
@Api("部门信息接口")
@RestController
@RequestMapping(value = "department")
public class SystemDepartmentController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(SystemDepartmentController.class);

    private DepartmentService departmentService;

    @Autowired
    public SystemDepartmentController(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    @ApiOperation(value = "返回匹配部门编号的部门信息")
    @GetMapping(value = "{id}", produces = MediaTypes.JSON_UTF_8)
    public Object details(
      @ApiParam(value = "部门编号", required = true) @PathVariable(required = true) String id
    ) {
        Department department = this.departmentService.findById(id);
        return result(department);
    }

    @ApiOperation(value = "返回部门信息分页列表")
    @GetMapping(value = "list", produces = MediaTypes.JSON_UTF_8)
    public Object list(
      @ApiParam(value = "页码(从0开始)", required = false) @RequestParam(required = false, defaultValue = "0") Integer page
    ) {
        Page<Department> departmentPage = this.departmentService.getList(super.pageRequest(page));
        return result(departmentPage);
    }

    @ApiOperation(value = "删除匹配部门编号的部门")
    @GetMapping(value = "delete/{id}", produces = MediaTypes.JSON_UTF_8)
    public Object delete(
      @ApiParam(value = "部门编号", required = true) @PathVariable(required = true) String id
    ) {
        this.departmentService.delete(id);
        return result();
    }

    @ApiOperation(value = "保存部门信息")
    @PostMapping(value = "save", consumes = MediaTypes.JSON_UTF_8)
    public Object save(
      @ApiParam(value = "部门信息实体", required = true) @RequestBody List<Department> departments
    ) {
        return result(this.departmentService.save(departments, super.currUser().getId()));
    }
}
